package com.whli.autumn.mall.wms.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.whli.autumn.core.exception.BusinessException;
import com.whli.autumn.core.extend.service.impl.BaseServiceImpl;
import com.whli.autumn.mall.wms.domain.dto.stock.StockQueryDTO;
import com.whli.autumn.mall.wms.domain.entity.Stock;
import com.whli.autumn.mall.wms.mapper.StockMapper;
import com.whli.autumn.mall.wms.service.IStockService;
import com.whli.autumn.mall.wms.domain.vo.stock.StockVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>库存信息业务实现</p>
 *
 * @author whli
 * @version 1.0.0
 * @since 2023-04-09 08:37
 */
@Service("stockService")
public class StockServiceImpl extends BaseServiceImpl<Stock, StockMapper> implements IStockService {

    @Override
    public List<StockVO> findByWarehouse(StockQueryDTO entity){
        if (entity == null || StringUtils.isEmpty(entity.getWarehouseInfoId())) {
            throw new BusinessException("仓库不能为空");
        }
        return mapper.findByWarehouse(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changeStockQty(String id, BigDecimal stockQty) {
        if (StringUtils.isEmpty(id) || stockQty == null) {
            throw new BusinessException("更改库存的数据和数量不能为空");
        }
        int rows = mapper.changeStockQty(id, stockQty);
        return SqlHelper.retBool(rows);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changeOccupiedQty(String id, BigDecimal occupiedQty) {
        if (StringUtils.isEmpty(id) || occupiedQty == null) {
            throw new BusinessException("更改占用库存的数据和数量不能为空");
        }
        int rows = mapper.changeOccupiedQty(id, occupiedQty);
        return SqlHelper.retBool(rows);
    }

}
